package com.net.xpay.common.service.impl;

import com.google.common.base.Strings;
import com.net.xpay.common.domain.SafePayBusinessAuthAuditRecord;
import com.net.xpay.common.domain.UserShard;
import com.net.xpay.common.manager.SafePayBusinessAuthAuditRecordManager;
import com.net.xpay.common.manager.UserShardManager;
import com.net.xpay.common.service.SafePayBusinessAuthAuditRecordService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.safePay.SafePayBookAccount;
import com.net.xpay.core.enums.SafePayBookAccountAuditStatus;
import com.net.xpay.core.manager.safePay.SafePayBookAccountManager;
import com.net.xpay.core.model.AdminSafePayBookAccountAuditModel;
import com.net.xpay.core.model.AdminSafePayBookAccountUpdateModel;
import com.net.xpay.core.validator.SafePayBookAccountSubmitBusinessUpdateValidator;
import com.net.common.exception.BusinessException;
import com.net.shard.ShardUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class SafePayBusinessAuthAuditRecordServiceImpl implements SafePayBusinessAuthAuditRecordService {

    @Autowired
    private SafePayBusinessAuthAuditRecordManager safePayBusinessAuthAuditRecordManager;

    @Autowired
    private SafePayBookAccountManager safePayBookAccountManager;

    @Autowired
    private UserShardManager userShardManager;

    @Autowired
    private SafePayBookAccountSubmitBusinessUpdateValidator submitBusinessUpdateValidator;

    @Override
    public void auditSuccess(Long id) {
        if (id == null || id == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "安全发账户审核记录id不可为空");
        }
        SafePayBusinessAuthAuditRecord safePayBusinessAuthAuditRecord = safePayBusinessAuthAuditRecordManager.getById(id);
        UserShard userShard = userShardManager.getById(safePayBusinessAuthAuditRecord.getUserId());
        try {
            ShardUtil.setShardContext(userShard.getShardId());
            SafePayBookAccount safePayBookAccount = safePayBookAccountManager.findByAlipayNickName(safePayBusinessAuthAuditRecord.getUserId(), safePayBusinessAuthAuditRecord.getAlipayNickName());
            SafePayBookAccountAuditStatus auditStatus = safePayBookAccount.getAuditStatus();
            if (auditStatus != SafePayBookAccountAuditStatus.AUDITING) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "审核状态异常");
            } else {
                safePayBookAccount.setAuditStatus(SafePayBookAccountAuditStatus.NORMAL);
                safePayBookAccount.setAuditTime(LocalDateTime.now());
                safePayBookAccountManager.updateByPrimaryKeyWithVersion(safePayBookAccount);

                safePayBusinessAuthAuditRecord.setAuditStatus(SafePayBookAccountAuditStatus.NORMAL);
                safePayBusinessAuthAuditRecord.setAuditTime(LocalDateTime.now());
                safePayBusinessAuthAuditRecordManager.update(safePayBusinessAuthAuditRecord);
            }
        } finally {
            ShardUtil.cleanShardContext();
        }

    }

    @Override
    public void auditFail(AdminSafePayBookAccountAuditModel model) {
        if (Strings.isNullOrEmpty(model.getAuditMessage())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "安全发账户审核失败原因不可为空");
        }
        if (model.getId() == null || model.getId() == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "安全发账户审核记录id不可为空");
        }

        SafePayBusinessAuthAuditRecord safePayBusinessAuthAuditRecord = safePayBusinessAuthAuditRecordManager.getById(model.getId());
        UserShard userShard = userShardManager.getById(safePayBusinessAuthAuditRecord.getUserId());
        try {
            ShardUtil.setShardContext(userShard.getShardId());
            SafePayBookAccount safePayBookAccount = safePayBookAccountManager.findByAlipayNickName(safePayBusinessAuthAuditRecord.getUserId(), safePayBusinessAuthAuditRecord.getAlipayNickName());
            SafePayBookAccountAuditStatus auditStatus = safePayBookAccount.getAuditStatus();
            if (auditStatus != SafePayBookAccountAuditStatus.AUDITING) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "审核状态异常");
            } else {
                safePayBookAccount.setAuditStatus(SafePayBookAccountAuditStatus.FAIL);
                safePayBookAccount.setAuditMessage(model.getAuditMessage());
                safePayBookAccount.setAuditTime(LocalDateTime.now());
                safePayBookAccountManager.updateByPrimaryKeyWithVersion(safePayBookAccount);

                safePayBusinessAuthAuditRecord.setAuditStatus(SafePayBookAccountAuditStatus.FAIL);
                safePayBusinessAuthAuditRecord.setAuditMessage(model.getAuditMessage());
                safePayBusinessAuthAuditRecord.setAuditTime(LocalDateTime.now());
                safePayBusinessAuthAuditRecordManager.update(safePayBusinessAuthAuditRecord);
            }
        } finally {
            ShardUtil.cleanShardContext();
        }
    }

    @Override
    public void updateInfo(AdminSafePayBookAccountUpdateModel model) {
        if (model.getId() == null || model.getId() == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "安全发账户审核记录id不可为空");
        }
        submitBusinessUpdateValidator.validateSafePayBookAccountSubmitBusinessAuth(model);
        SafePayBusinessAuthAuditRecord safePayBusinessAuthAuditRecord = safePayBusinessAuthAuditRecordManager.getById(model.getId());
        UserShard userShard = userShardManager.getById(safePayBusinessAuthAuditRecord.getUserId());
        try {
            ShardUtil.setShardContext(userShard.getShardId());
            SafePayBookAccount safePayBookAccount = safePayBookAccountManager.findByAlipayNickName(safePayBusinessAuthAuditRecord.getUserId(), safePayBusinessAuthAuditRecord.getAlipayNickName());
            SafePayBookAccountAuditStatus auditStatus = safePayBookAccount.getAuditStatus();
            if (auditStatus == SafePayBookAccountAuditStatus.INIT ||auditStatus == SafePayBookAccountAuditStatus.AUDITING || auditStatus == SafePayBookAccountAuditStatus.NORMAL) {

                safePayBookAccount.setBusinessName(model.getBusinessName());
                safePayBookAccount.setIcpInfo(model.getIcpInfo());
                safePayBookAccount.setIdCardFront(model.getIdCardFront());
                safePayBookAccount.setIdCardBack(model.getIdCardBack());
                safePayBookAccount.setBusinessLicense(model.getBusinessLicense());
                safePayBookAccount.setOfficeImages(model.getOfficeImages());
                safePayBookAccount.setBizSceneDesc(model.getBizSceneDesc());
                safePayBookAccount.setMerchantCommitment(model.getMerchantCommitment());
                if(auditStatus == SafePayBookAccountAuditStatus.INIT){
                    safePayBookAccount.setAuditStatus(SafePayBookAccountAuditStatus.AUDITING);
                }

                safePayBookAccountManager.updateByPrimaryKeyWithVersion(safePayBookAccount);

                safePayBusinessAuthAuditRecord.setBusinessName(model.getBusinessName());
                safePayBusinessAuthAuditRecord.setBizLicense(model.getBizLicense());
                safePayBusinessAuthAuditRecord.setIcpInfo(model.getIcpInfo());
                safePayBusinessAuthAuditRecord.setIdCardFront(model.getIdCardFront());
                safePayBusinessAuthAuditRecord.setIdCardBack(model.getIdCardBack());
                safePayBusinessAuthAuditRecord.setBusinessLicense(model.getBusinessLicense());
                safePayBusinessAuthAuditRecord.setOfficeImages(model.getOfficeImages());
                safePayBusinessAuthAuditRecord.setBizSceneDesc(model.getBizSceneDesc());

                safePayBusinessAuthAuditRecord.setOtherOneImages(model.getOtherOneImages());
                safePayBusinessAuthAuditRecord.setOtherTwoImages(model.getOtherTwoImages());
                safePayBusinessAuthAuditRecord.setOtherThreeImages(model.getOtherThreeImages());

                safePayBusinessAuthAuditRecord.setMerchantCommitment(model.getMerchantCommitment());
                if(auditStatus == SafePayBookAccountAuditStatus.INIT){
                    safePayBusinessAuthAuditRecord.setAuditStatus(SafePayBookAccountAuditStatus.AUDITING);
                }

                safePayBusinessAuthAuditRecordManager.update(safePayBusinessAuthAuditRecord);
            } else {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "审核状态异常");
            }
        } finally {
            ShardUtil.cleanShardContext();
        }

    }
}
